﻿using AutoMapper;
using IService.Managemen;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Models.Managemen;
using ModelToDto.Managemen;
using SqlSugar;
using System.Collections.Generic;
using System.Security.Claims;
using static System.Net.Mime.MediaTypeNames;

namespace ManagementPlatform.Utillity.Filters
{
    public class MenueFilter : IActionFilter
    {
        public IMenueService IMenueService { get; set; }
       public IMapper mapper { get; set; }
        public MenueFilter(IMenueService IMenueService, IMapper mapper)
        {
            this.IMenueService = IMenueService;
            this.mapper = mapper;
        }



        public void OnActionExecuting(ActionExecutingContext context)
        {
          
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Result is ViewResult)
            {
                var user = context.HttpContext.User;
                Claim currentclaim = user.Claims.FirstOrDefault(c => c.Type == "Userid");
                int currentUserId = 0;
                if (currentclaim != null)
                {
                    currentUserId = Convert.ToInt32(currentclaim.Value);
                }
                var exp = Expressionable.Create<Mp_Menue>();
              
                var toExp = exp.ToExpression();
                List<Mp_Menue> menueInfodtoList =  IMenueService.GetMenueList(toExp, 0); ;
                var toDto = mapper.Map<List<Mp_Menue>, List<MenueDTO>>(menueInfodtoList);
                ViewResult result = (ViewResult)context.Result;
                result.ViewData.Add("MenueAllList", toDto);
                context.Result = result;
            }
        }
    }
}
